import { isAuth } from "@/utils/storage"
import React from "react"
import { useLocation } from "react-router-dom"
import { Redirect } from "react-router-dom"
import { Route } from "react-router-dom"

// 解构需要的属性 除了component
export default function PrivateRoute({ children, component: Component, ...rest }) {
  const location = useLocation()
  return (
    <Route
      {...rest}
      render={() => {
        if (isAuth()) {
          return children ? children : <Component></Component>
        } else {
          return (
            <Redirect
              to={{
                pathname: "/login",
                // 通过state传递额外参数
                state: {
                  from: location.pathname,
                },
              }}></Redirect>
          )
        }
      }}></Route>
  )
}
